package com.share.netdisk.file.controller;

import com.share.constant.Result;
import com.share.netdisk.file.service.FileService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;

@RestController
@RequestMapping("/file")
@Slf4j
public class FileController {

    @Autowired
    private FileService fileService;
    // 上传文件
    @PostMapping(value = "/upload")
    public Result upload(@RequestPart("file") MultipartFile file, @RequestParam("parentId") Long parentId) {
        log.info("controller的file:{}", file);
        return fileService.add(file, parentId);
    }
    //TODO 下载文件 成功
    @GetMapping(value = "/download")
    public ResponseEntity<byte[]> download(@RequestParam("fileId") Long fileId,  HttpServletResponse response) {
        log.info("fileId:{}", fileId);
        return fileService.download(fileId,response);
    }

    //TODO 文件移动
    @PutMapping( "/move")
    public Result move(@RequestParam("srcFileId") Long srcFileId,@RequestParam("dstFileId") Long dstFileId) {
        log.info("fileId:{},parentId:{}", srcFileId, dstFileId);
        return fileService.moveFile(srcFileId, dstFileId);

    }

    //TODO 文件复制 成功
    @PutMapping( "/copy")
    public Result copy(@RequestParam("srcFileId") Long srcFileId,@RequestParam("dstFileId") Long dstFileId) {
        log.info("fileId:{},parentId:{}", srcFileId, dstFileId);
        return fileService.copyFile(srcFileId, dstFileId);

    }

    //TODO 删除文件（逻辑删除） 成功
    @DeleteMapping( "/delete")
    public Result delete(@RequestParam("fileId") Long fileId) {
        log.info("fileId:{}", fileId);
        return fileService.removeFileById(fileId);
    }

    //TODO 文件重命名  成功
    @PutMapping("/rename")
    public Result rename(@RequestParam("fileId") Long fileId, @RequestParam("newName") String newName) {
        log.info("fileId:{},newName:{}", fileId, newName);
        return fileService.renameFile(fileId, newName);
    }

    //TODO 获取文件列表 成功
    @GetMapping("/getlist")
    public Result list(@RequestParam(value = "fileId" ,required = false) Long fileId, @RequestParam(value = "parentId",required = false) Long parentId,@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        log.info("LIST:parentId:{},fileId:{},pageNum:{},pageSize:{}", parentId, fileId, pageNum, pageSize);
        return fileService.fileList(fileId, parentId,pageNum, pageSize);
    }

    // TODO 创建文件  成功
    @PostMapping("/create")
    public Result create(@RequestParam(value = "folderName",required = false) String fileName,@RequestParam(value = "parentId",required = false) Long parentId) {
        log.info("fileName:{},parentId:{}", fileName, parentId);
        return fileService.createFile(fileName,parentId);
    }

    //搜索文件
    @GetMapping("/search")
    public Result search(@RequestParam("fileName") String fileName,@RequestParam(value = "parentId",required = false) Long parentId,@RequestParam("pageNum") int pageNum, @RequestParam("pageSize") int pageSize) {
        log.info("fileName:{},parentId:{},pageNum:{},pageSize:{}", fileName, parentId,pageNum,pageSize);
        return fileService.searchFile(fileName,parentId,pageNum,pageSize);
    }

}
